package tech.eyao.hs60x.dao;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.enums.Delete;
import tech.eyao.hs60x.mapper.AccountRelationDOMapper;
import tech.eyao.hs60x.model.domain.AccountRelationDO;

@Repository
public class AccountRelationDao {

    @Autowired
    private AccountRelationDOMapper accountRelationDOMapper;

    /**
     * 是否绑定手表
     * 
     * @param userId
     * @param watchId
     * @return
     */
    public boolean isBindWatch(long userId, long watchId) {
        return null != accountRelationDOMapper.getAccountRelation(userId, watchId);
    }

    /**
     * 是否为管理用户
     * 
     * @param userId
     * @return
     */
    public boolean isAdmin(long userId) {
        return null != accountRelationDOMapper.getAccountAdminByAccountId(userId);
    }

    /**
     * 查询用户绑定手表关系
     * 
     * @param userId
     * @param watchId
     * @return
     */
    public AccountRelationDO getAccountRelation(long userId, long watchId) {
        return accountRelationDOMapper.getAccountRelation(userId, watchId);
    }

    /**
     * 绑定手表
     * 
     * @param accountRelationDO
     * @return
     */
    public boolean insert(AccountRelationDO accountRelationDO) {
        this.setCommonInsertParamInit(accountRelationDO);
        return accountRelationDOMapper.insert(accountRelationDO) > 0;
    }

    /**
     * 查询手表管理员绑定关系
     * 
     * @param wacthAccountId
     * @return
     */
    public AccountRelationDO getAccountAdminByWatchId(Long wacthAccountId) {
        return accountRelationDOMapper.getAccountAdminByWatchId(wacthAccountId);
    }

    /**
     * 绑定手表列表
     * 
     * @param userId
     * @return
     */
    public List<AccountRelationDO> getAccountRelationByAccountId(long userId) {
        return accountRelationDOMapper.getAccountRelationByAccountId(userId);
    }

    /**
     * 获取手表绑定关系列表
     * 
     * @param watchId
     * @return
     */
    public List<AccountRelationDO> getWatchRelationList(long watchId) {
        return accountRelationDOMapper.getWatchRelationList(watchId);
    }

    /**
     * 解绑手表
     * 
     * @param watchId
     * @param userId
     * @return
     */
    public boolean unbindWatch(long watchId, long userId) {
        return accountRelationDOMapper.unbindWatch(watchId, userId) > 0;
    }

    public boolean delete(Long id) {
        return accountRelationDOMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 更新关系存储
     * 
     * @param accountRelation
     * @return
     */
    public boolean updateRelationByPrimaryKey(AccountRelationDO accountRelationDO) {
        this.setCommonUpdateParamInit(accountRelationDO);
        return accountRelationDOMapper.updateByPrimaryKey(accountRelationDO) > 0;
    }

    /**
     * 设置公共信息-入库
     * 
     * @param accountRelationDO
     * @return
     */
    private AccountRelationDO setCommonInsertParamInit(AccountRelationDO accountRelationDO) {
        accountRelationDO.setGmtCreated(new Date());
        accountRelationDO.setGmtModified(new Date());
        accountRelationDO.setCreator(CommonConstants.SYSTEM_USER);
        accountRelationDO.setModifier(CommonConstants.SYSTEM_USER);
        accountRelationDO.setIsDeleted(Delete.N.getValue());
        return accountRelationDO;
    }

    /**
     * 设置公共信息-更新
     * 
     * @param accountRelationDO
     * @return
     */
    private AccountRelationDO setCommonUpdateParamInit(AccountRelationDO accountRelationDO) {
        accountRelationDO.setGmtModified(new Date());
        accountRelationDO.setModifier(CommonConstants.SYSTEM_USER);
        return accountRelationDO;
    }
}
